<template>
  <div class="page-container">
    <!--表格内容栏-->
    <ac-table
      ref="dataTable" 
      pkName="id" 
      :filters="filters"
      :filterFields="filterFields" 
      :columns="columns"
      :defaultSort="defaultSort"
      @handleCellOperator="handleCellOperator"
      @handleTableOperator="handleTableOperator"
    ></ac-table>

    <!--新增编辑界面-->
    <el-dialog
      :title="operator"
      width="1000px"
      v-model="dialogVisible"
      :close-on-click-modal="false"
      append-to-body
      destroy-on-close
      draggable
    >
      <el-form
        :model="formData"
        label-width="120px"
        :rules="rules"
        ref="dataForm"
        label-position="right"
        :disabled="!dialogEdit"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="类型" prop="type">
              <ac-dict-radio v-model="formData.type" dictCode="dictMessageType" />
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="formData.type !== '3'">
            <el-form-item label="接收系统" prop="systemIds">
              <ac-selects v-model="formData.systemIds" :options="subsystemOptions" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="formData.type !== '1'">
          <el-col :span="24">
            <el-form-item label="接收范围" prop="receiveType">
              <ac-dict-radio v-model="formData.receiveType" dictCode="dictMessageRange" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="formData.type !== '1' && formData.receiveType === '2'">
          <el-col :span="24">
            <el-form-item label="接收人员" prop="receiverIds">
              <ac-employees-tree v-model="formData.receiverIds" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="标题" prop="title">
              <ac-input v-model="formData.title" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="内容" prop="content">
              <ac-rich-editor v-model="formData.content" style="height: 400px;"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <el-button
          v-if="dialogEdit"
          size="default"
          type="info"
          @click="dialogVisible = false"
        >取消</el-button>
        <el-button
          v-if="dialogEdit"
          size="default"
          type="primary"
          @click="submitForm"
          :loading="editLoading"
        >提交</el-button>
        <el-button
          v-if="!dialogEdit"
          size="default"
          type="info"
          @click="dialogVisible = false"
        >关闭</el-button>
      </template>
    </el-dialog>

    <!--查看界面-->
    <el-dialog
      :title="viewOperator"
      v-model="viewVisible"
      :close-on-click-modal="false"
      append-to-body
      destroy-on-close
      draggable
      fullscreen
    >
      <el-tabs v-model="viewTabName" class="fit-box full-height">
        <el-tab-pane name="form" label="基础信息" class="fit-box full-height">
          <el-form
            :model="formData"
            ref="viewForm"
            label-width="120px"
            label-position="right"
            disabled
          >
            <el-row>
              <el-col :span="11">
                <el-form-item label="类型" prop="type">
                  <ac-dict-radio v-model="formData.type" dictCode="dictMessageType" />
                </el-form-item>
              </el-col>
              <el-col :span="11" v-if="formData.type !== '3'">
                <el-form-item label="接收系统" prop="systemIds">
                  <ac-selects v-model="formData.systemIds" :options="subsystemOptions" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row v-if="formData.type !== '1'">
              <el-col :span="22">
                <el-form-item label="接收范围" prop="receiveType">
                  <ac-dict-radio v-model="formData.receiveType" dictCode="dictMessageRange" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="22">
                <el-form-item label="标题" prop="title">
                  <ac-input v-model="formData.title" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="22">
                <el-form-item label="内容" prop="content">
                  <ac-rich-editor v-model="formData.content" style="height: 400px;"/>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="11">
                <el-form-item label="更新时间" prop="updateTime">
                  <ac-datetime v-model="formData.updateTime" />
                </el-form-item>
              </el-col>
              <el-col :span="11">
                <el-form-item label="创建时间" prop="createTime">
                  <ac-datetime v-model="formData.createTime" />
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-tab-pane>
        <el-tab-pane name="table" label="查看情况" class="fit-box full-height">
          <ac-table
            ref="viewDataTable"
            pkName="id"
            class="fit-table"
            dataUrl="/system/messageStatus/queryPage"
            :filters="viewFilters"
            :filterFields="viewFilterFields"
            :columns="viewColumns"
          ></ac-table>
        </el-tab-pane>
      </el-tabs>
      <template #footer>
        <el-button
          size="default"
          type="info"
          @click="viewVisible = false"
        >关闭</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, nextTick, onMounted } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import api from '@/http/api';

const filters = ref({ name: '' });
const filterFields = ref([
  {prop: "title", label: "标题"},
  {prop: "type", label: "类型", type: 'dict-select', dictCode: 'dictMessageType'},
]);
const columns = ref([
  {prop: "title", label: "标题", minWidth: 200, click: true, align: 'left'},
  {prop: "type", label: "类型", width: 120, type: 'dict-select', dictCode: 'dictMessageType'},
  {prop: "receiveType", label: "接收范围", width: 120, type: 'dict-select', dictCode: 'dictMessageRange'},
  {prop: "updateTime", label: "更新时间", width: 160, type: "datetime"},
  {prop: "createTime", label: "创建时间", width: 160, type: "datetime"},
]);
const operator = ref('');
const dialogVisible = ref(false);
const editLoading = ref(false);
const dialogEdit = ref(false);
const formData = ref({});
const rules = ref({
  type: [{required: true, message: '请选择类型', trigger: 'blur'}],
  systemIds: [{required: true, message: '请选择接收系统', trigger: 'blur'}],
  receiveType: [{required: true, message: '请选择接收范围', trigger: 'blur'}],
  receiverIds: [{required: true, message: '请选择接收人员', trigger: 'blur'}],
  title: [{required: true, message: '请输入标题', trigger: 'blur'}],
  content: [{required: true, message: '请输入内容', trigger: 'blur'}],
});
const defaultSort = ref({});
const dataForm = ref();
const dataTable = ref();
const subsystemOptions = ref([]);

const viewOperator = ref('');
const viewVisible = ref(false);
const viewForm  = ref();
const viewTabName = ref('form');
const viewFilters = ref({ title: '' });
const viewFilterFields = ref([
  {prop: "employeeName", label: "姓名"},
]);
const viewColumns = ref([
  {prop: "employeeName", label: "姓名", minWidth: 200, align: 'left'},
  {prop: "ifRead", label: "是否已读", width: 120, type: 'dict-select', dictCode: 'dictYesNo'},
  {prop: "readTime", label: "阅读时间", width: 160, type: "datetime"},
]);

const handleCellOperator = (property, value, row) => {
  formData.value = Object.assign({}, row);
  viewVisible.value = true;
  viewOperator.value = "查看";
  viewTabName.value = 'form';
  viewFilters.value.messageId = row.id;

  nextTick(() => {
    viewForm.value && viewForm.value.clearValidate();
  });
};

const handleTableOperator = (button, row) => {
  dialogEdit.value = true;
  operator.value = button.name;
  if (button.buttonType === 'edit') {
    api.post("/system/message/queryBy", { id: row.id }).then((res) => {
      dialogVisible.value = true
      formData.value = Object.assign({}, res.data, {button: button});
      nextTick(() => dataForm.value && dataForm.value.clearValidate());
      editLoading.value = false;
    });
  } else {
    dialogVisible.value = true
    formData.value = {type: '1', receiveType: '1', button: button};
    nextTick(() => dataForm.value && dataForm.value.clearValidate());
  }
};

const submitForm = () => {
  dataForm.value && dataForm.value.validate((valid) => {
    if (valid) {
      ElMessageBox.confirm('请确认是否提交？', '提示', {type: 'info'}).then(() => {
        editLoading.value = true
        api.post(formData.value.button.dataDestUrl, formData.value).then((res) => {
          ElMessage.success(res.message);
          dialogVisible.value = false;
          dataTable.value.findPage()
          editLoading.value = false;
        }).catch(() => {
          editLoading.value = false;
        });
      }).catch(()=>{});
    }
  });
};

const loadSubsystem = () => {
  api.post("/system/subsystem/queryList", {type: '3'}).then(res => {
    res.data.forEach(data => {
      subsystemOptions.value.push({
        key: data.id,
        value: data.name
      });
    });
  });
};

onMounted(() => {
  loadSubsystem();
});
</script>

<style scoped>
</style>